<?php
namespace api\modules\ua\models;

use Yii;

class Baidu
{
    public function newBaiduToken(){
        $url = Yii::$app->params['ua']['conf']['baiduAccessTokenURL'];

        $cache = Yii::$app->cache;
        $curl = new Request();
        $response = $curl->curlPost($url);
        $token = json_decode($response);

        // 将 $data 存放到缓存供下次使用
        $cache->set('BaiduAipToken', $token, 864000);
        return $token;
    }

    public function getBaiduToken(){
        $cache = Yii::$app->cache;
        $token = $cache->get('BaiduAipToken');

        if ($token === false || isset($token->error)) {
            $token = $this->newBaiduToken();
        }

        return $token->access_token;
    }
}
